# coding: utf-8
from sqlalchemy import Column, DateTime, Float, ForeignKey, Integer, LargeBinary, String
from sqlalchemy.schema import FetchedValue
from sqlalchemy.orm import relationship
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()


class DepartmentInfo(db.Model):
    __tablename__ = 'department_info'

    DepartmentID = db.Column(db.String(20, 'utf8mb4_0900_ai_ci'), primary_key=True)
    DepartmentName = db.Column(db.String(255, 'utf8mb4_0900_ai_ci'), nullable=False)
    CreateTime = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())


class EmployeeInfo(db.Model):
    __tablename__ = 'employee_info'

    UserID = db.Column(db.String(20), primary_key=True)
    UserName = db.Column(db.String(255), nullable=False)
    DepartmentID = db.Column(db.ForeignKey('department_info.DepartmentID', ondelete='CASCADE', onupdate='CASCADE'), nullable=False, index=True)
    CreateTime = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())

    department_info = db.relationship('DepartmentInfo', primaryjoin='EmployeeInfo.DepartmentID == DepartmentInfo.DepartmentID', backref='employee_infos')


class NewspaperInfo(db.Model):
    __tablename__ = 'newspaper_info'

    NewspaperID = db.Column(db.String(20, 'utf8mb4_0900_ai_ci'), primary_key=True)
    NewspaperName = db.Column(db.String(255, 'utf8mb4_0900_ai_ci'), nullable=False)
    OtherInfo = db.Column(db.String(255, 'utf8mb4_0900_ai_ci'), nullable=False)
    Price = db.Column(db.Float(20), nullable=False)
    CreateTime = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())


class OrderInfo(db.Model):
    __tablename__ = 'order_info'

    AutoID = db.Column(db.Integer, primary_key=True)
    UserID = db.Column(db.ForeignKey('employee_info.UserID', ondelete='CASCADE', onupdate='CASCADE'), nullable=False, index=True, info='用户主键')
    NewspaperID = db.Column(db.ForeignKey('newspaper_info.NewspaperID', ondelete='CASCADE', onupdate='CASCADE'), nullable=False, index=True)
    Number = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    CreateTime = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())

    newspaper_info = db.relationship('NewspaperInfo', primaryjoin='OrderInfo.NewspaperID == NewspaperInfo.NewspaperID', backref='order_infos')
    employee_info = db.relationship('EmployeeInfo', primaryjoin='OrderInfo.UserID == EmployeeInfo.UserID', backref='order_infos')


class OrderLog(db.Model):
    __tablename__ = 'order_log'

    AutoID = db.Column(db.Integer, primary_key=True)
    UserID = db.Column(db.String(20), nullable=False)
    UserName = db.Column(db.String(255, 'utf8mb4_0900_ai_ci'), nullable=False)
    DepartmentID = db.Column(db.String(20, 'utf8mb4_0900_ai_ci'), nullable=False)
    DepartmentName = db.Column(db.String(255, 'utf8mb4_0900_ai_ci'), nullable=False)
    NewspaperID = db.Column(db.String(20, 'utf8mb4_0900_ai_ci'), nullable=False)
    NewspaperName = db.Column(db.String(255, 'utf8mb4_0900_ai_ci'), nullable=False)
    CreateTime = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    OptionType = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='1 --- 订阅 2 -- 修改订阅3--取消订阅')

